package com.youdianstar.app.ui.op

import androidx.databinding.ObservableField
import androidx.lifecycle.viewModelScope
import com.youdianstar.app.common.base.BaseViewModel
import com.youdianstar.app.common.entity.FileUpResult
import com.youdianstar.app.common.event.NetStateLiveData
import com.youdianstar.app.entity.*
import com.youdianstar.app.support.ApiRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
 * File: OPViewModel
 * Author: 82149 Create: 2023/8/1 16:58
 * Changes (from 2023/8/1)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class OPMerchantViewModel(private val repo: ApiRepository) : BaseViewModel() {
    val accountDataField = ObservableField<String>()

    val merchantDataField = ObservableField<OpMerchantHome>()

    val amountInputField = ObservableField<String>()

    val centerStatisticsLiveData = NetStateLiveData<OpMerchantHome>()

    /**
     * 首页统计
     * @param branchId 网点id
     */
    fun getMerchantCenterCount(branchId:Int?,isShow:Boolean = false,rangeTime: String? =null) {
        if (isShow)
            showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getMerchantCenterCount(branchId,rangeTime,centerStatisticsLiveData)
        }
    }

    val withdrawalResultLiveData = NetStateLiveData<Any>()
    /**
     * 商户中心--提现
     *
     * @param req 请求信息
     */
    fun requestWithdrawal(req: WithdrawsReq){
        showLoading()
        viewModelScope.launch (Dispatchers.IO){
            repo.requestWithdrawal(req,withdrawalResultLiveData)
        }
    }

    val withdrawalAccountLiveData = NetStateLiveData<List<CardEntity>>()

    /**
     * 商户中心
     * --获取关联账户
     */
    fun getWithdrawalAccount(){
        showLoading()
        viewModelScope.launch (Dispatchers.IO){
            repo.getWithdrawalAccount(withdrawalAccountLiveData)
        }
    }

    //订单列表
    val orderResLiveData = NetStateLiveData<List<Any>>()

    /**
     * 商户中心
     * --订单列表
     * @param offset 偏移量 0开始
     */
    fun getMerchantOrders(branchId:Int?,offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getMerchantOrders(branchId,offset = offset, orderResLiveData)
        }
    }

    //分成记录
    val transRecordLiveData = NetStateLiveData<List<OpTransaction>>()

    /**
     * 商户中心--分成流水
     *
     * @param offset 偏移量 0开始
     */
    fun getMerchantTrans(branchId:Int?,offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getMerchantTrans(branchId,offset = offset, transRecordLiveData)
        }
    }

    val transStatisticsLiveData = NetStateLiveData<OpTransCount>()

    /**
     * 获取分成统计
     *
     * @param branchId 网点id
     */
    fun getMerchantTransCount(branchId:Int?) {
        viewModelScope.launch(Dispatchers.IO) {
            repo.getMerchantTransCount(branchId,transStatisticsLiveData)
        }
    }

    //提现记录
    val withdrawalRecordLiveData = NetStateLiveData<List<WithdrawalRec>>()

    /**
     * 商户中心--提现记录
     *
     * @param offset 偏移量 0开始
     */
    fun getWithdrawalRecord(branchId:Int?,offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getWithdrawalRecord(branchId, offset = offset, withdrawalRecordLiveData)
        }
    }

    //设备列表
    val deviceListLiveData = NetStateLiveData<List<DeviceEntity>>()

    /**
     * 商户中心
     * --设备列表
     * @param offset 偏移量 0开始
     */
    fun getDeviceList(branchId:Int?,offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getDeviceList(branchId ,offset = offset, deviceListLiveData)
        }
    }


    //------------------------------------网点编辑-------------------------------

    val branchNameField = ObservableField<String>()
    val branchAddressField = ObservableField<String>()
    val branchPhoneField = ObservableField<String>()
    val contactNameField = ObservableField<String>()
    val shopConfigListLiveData = NetStateLiveData<List<MerchantOP>>()

    /**
     * 获取关联网点列表
     * --主页筛选
     * @param isShow 显示
     */
    fun getShopConfigList(isShow: Boolean){
        if (isShow)
            showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getShopList(shopConfigListLiveData)
        }
    }

    val shopDetailLiveData = NetStateLiveData<OpShopInfo>()

    /**
     * 商户中心--获取网点详情
     *
     * @param branchId 网点id
     */
    fun getShopDetails(branchId: Int){
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getShopDetails(branchId, shopDetailLiveData)
        }
    }

    val editDetailLiveData = NetStateLiveData<Any>()

    /**
     * 更改网点信息
     *
     * @param req 请求数据
     */
    fun saveShopDetailUpdate(req:OpShopRequest){
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.updateShopDetail(req,editDetailLiveData)
        }
    }

    //行业列表
    val industriesLiveData = NetStateLiveData<List<OpIndustry>>()
    /**
     * 商户中心
     * --获取行业列表
     */
    fun getShopIndustries(){
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getShopIndustries(industriesLiveData)
        }
    }

    val fileUpLoadLiveData = NetStateLiveData<FileUpResult>()

    val fileDeleteLiveData = NetStateLiveData<Any>()

    /**上传图片*/
    fun upLoadImage(path: String){
        showLoading()
        viewModelScope.launch {
            repo.upLoadImage(path,fileUpLoadLiveData)
        }
    }

    /**删除服务端图片*/
    fun deleteImage(url:String){
        viewModelScope.launch {
            repo.deleteImage(url,fileDeleteLiveData)
        }
    }

}